package com.kaifamiao.controller;

import com.kaifamiao.pojo.Doctor;
import com.kaifamiao.pojo.Department;
import com.kaifamiao.service.imp.DoctorService;
import com.kaifamiao.service.imp.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/doctor")
public class DoctorController {
    @Autowired
    DoctorService doctorService = null;
    @Autowired
    DepartmentService departmentService = null;

    @RequestMapping("/patient/index")
    public String index(Model model){
        List<Department> departmentList = departmentService.findAll();
        Department department = new Department();
        department.setDepartid(0);
        department.setDepartname("请先选择科室");
        departmentList.add(0,department);

        List<Doctor> doctorList = doctorService.findAll();
        Doctor doctor = new Doctor();
        doctor.setDoctor_id(0);
        doctor.setDoctor_name("请选择您要预约的医生");
        doctorList.add(0,doctor);

        model.addAttribute("departmentList",departmentList);
        model.addAttribute("doctorList",doctorList);
        return "Patientinfohtml/index";
    }

    @RequestMapping("/index")
    public String doctorIndex(){
        return "doctor/patient-list";
    }

    @RequestMapping("/findall")
    public String findall(Model model){
        List<Doctor> doctorList = doctorService.findAll();
        System.out.println(doctorList);
        model.addAttribute("doctors",doctorList);
        return "doctor/findall";
    }
    @RequestMapping("/findByDepartmentId/{id}")
    public @ResponseBody List<Doctor> findByDepartmentId(@PathVariable int id){
        System.out.println(id);
//        model.addAttribute("doctorList",doctorService.findByDepartmentId(id));
//      return doctorService.findByDepartmentId(id);
        List<Doctor> list = doctorService.findByDepartmentId(id);
        System.out.println(list.toString());
        return list;
    }
//
//    @RequestMapping("/findByUsername/{username}")
//    public String findByUsername(Model model,@PathVariable("username") String username){
//        System.out.println(username);
//        Doctor doctor = doctorService.findByUsername(username);
//        System.out.println(doctor);
//        return "doctor";
//    }
//    @RequestMapping("/add")
    public String add(Model model){
        Doctor doctor = new Doctor();
        model.addAttribute("doctor",doctor);
        return "doctor/add";
    }
    @RequestMapping("/doadd")
    public String doAdd(Model model,Doctor doctor){
        System.out.println(doctor);
        int id = doctor.getDoctor_id();
        if(doctorService.findById(id) != null){
            model.addAttribute("doctor",doctor);
            model.addAttribute("adderr","已存在该医生编号，请重新输入");
        }else{
            if (doctorService.addDoctor(doctor)){
                model.addAttribute("doctor",doctor);
                model.addAttribute("add","添加成功");
            }
            else
                model.addAttribute("adderr","添加失败");
        }
        return "doctor/add";
    }

    @RequestMapping("/edit/{id}")
    public String edit(Model model,@PathVariable("id") int id){
        Doctor doctor = doctorService.findById(id);
        model.addAttribute("doctor",doctor);
        return "doctor/edit";
    }
    @RequestMapping("/doedit")
    public String doedit(Model model,Doctor doctor){
        System.out.println(doctor);
        if (doctorService.update(doctor)){
            model.addAttribute("doctor",doctor);
            model.addAttribute("edit","修改成功");
        }
        else
            model.addAttribute("editerr","修改失败");
        return "doctor/edit";
    }

    @RequestMapping("/delete/{id}")
    public String delete(Model model,@PathVariable("id") int id){
        System.out.println(id);
        if (doctorService.delete(id)){
            model.addAttribute("del","删除成功");
            return "redirect:/doctor/findall";
        }
        else
            model.addAttribute("delerr","删除失败");
            return "redirect:/doctor/findall";
    }
}
